//
//  OMTAlbumTitleView.m
//  OMTImagePicker
//
//  Created by yizhaorong on 2017/11/16.
//

#import "OMTAlbumTitleView.h"
#import "OMTImagePickerController.h"

@interface OMTAlbumTitleView ()
// 标题
@property (nonatomic, strong) UILabel *titleLabel;
// 提示
@property (nonatomic, strong) UILabel *tipLabel;
// 图标
@property (nonatomic, strong) UIImageView *iconView;

@end

@implementation OMTAlbumTitleView

#pragma mark - Lifecycle
- (void)dealloc {
    // [self unregisterNotifications]
}

- (instancetype)initWithAlbumManager:(OMTAlbumManager *)albumManager {
    if (self = [super init]) {
        self.albumManager = albumManager;
        [self setupSubviews];
        [self setLayout];
    }
    return self;
}

#pragma mark - View Lifecycle
- (void)setupSubviews {
    
    NSString *(^Album18N)(NSString *) = [self.albumManager.builder.localizedString copy];
    // 初始化子视图
    _titleLabel = [UILabel new];
    _titleLabel.font = [UIFont boldSystemFontOfSize:14];
    _titleLabel.textColor = [UIColor whiteColor];
    [self addSubview:_titleLabel];
    
    _tipLabel = [UILabel new];
    _tipLabel.text = Album18N(@"omt_album_tap_changed");
    _tipLabel.font = [UIFont boldSystemFontOfSize:9];
    _tipLabel.textColor = [UIColor whiteColor];
    [self addSubview:_tipLabel];
    
    _iconView = [UIImageView new];
    [self addSubview:_iconView];
}

- (void)setLayout {
    // 设置或更新布局
    CGFloat width = self.bounds.size.width;
    CGFloat height = self.bounds.size.height;
    
    CGSize titleSize = [self.titleLabel sizeThatFits:CGSizeMake(width, height)];
    CGFloat titleH = titleSize.height;
    CGFloat titleW = titleSize.width;
    CGFloat titleY = 8;
    CGFloat titleX = (width - titleW) / 2;
    CGRect titleFrame = CGRectMake(titleX, titleY, titleW, titleH);
    self.titleLabel.frame = titleFrame;
    
    CGSize tipSize = [self.tipLabel sizeThatFits:CGSizeMake(width, height)];
    CGFloat tipH = tipSize.height;
    CGFloat tipW = tipSize.width;
    CGFloat tipY = CGRectGetMaxY(titleFrame);
    CGFloat tipX = (width - tipW) / 2;
    CGRect tipFrame = CGRectMake(tipX, tipY, tipW, tipH);
    self.tipLabel.frame = tipFrame;
}

- (CGSize)sizeThatFits:(CGSize)size {
    CGSize titleSize = [self.titleLabel sizeThatFits:CGSizeMake(200, MAXFLOAT)];
    CGSize tipSize = [self.tipLabel sizeThatFits:CGSizeMake(200, MAXFLOAT)];
    return CGSizeMake(MAX(titleSize.width, tipSize.width), tipSize.height + titleSize.height);
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self setLayout];
    self.titleLabel.textColor = self.albumManager.builder.normalColor;
    self.tipLabel.textColor = self.albumManager.builder.normalColor;
}

#pragma mark - Notifications
/*
 - (void)registerNotifications {
 // 注册通知
 }
 
 - (void)unregisterNotifications {
 // 注销通知
 }
 */
#pragma mark - Actions

#pragma mark - Private

#pragma mark - Public


#pragma mark - Protocol conformance


#pragma mark - Lazyload


#pragma mark - Custom Accessors
//自定义 setter getter
- (void)setTitle:(NSString *)title {
    _titleLabel.text = title;
    [self setLayout];
}

@end
